package src;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import src.AddDialog;
import src.CustomerInfoPanel;
import src.WaterTypeInfoPanel;
import src.SalesmanInfoPanel;
import src.UsageInfoPanel;
import src.PaymentInfoPanel;
import src.WaterTypeAddDialog;
import src.CustomerAddDialog;
import src.SalesmanAddDialog;
import src.PaymentAddDialog;
import src.UsageAddDialog;
import src.StyleConstants;

public class WaterBillingSystem extends JFrame {
    private Connection connection;
    private JButton customerButton, usageButton, paymentButton, salesmanButton, waterTypeButton;

    public WaterBillingSystem() {
        setTitle("水费管理系统");
        setSize(800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        getContentPane().setBackground(StyleConstants.BACKGROUND_COLOR);

        // 创建主面板
        JPanel mainPanel = new JPanel(new BorderLayout(10, 10));
        mainPanel.setBackground(StyleConstants.BACKGROUND_COLOR);
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        // 创建标题面板
        JPanel titlePanel = new JPanel();
        titlePanel.setBackground(StyleConstants.THEME_COLOR);
        JLabel titleLabel = new JLabel("水费管理系统", JLabel.CENTER);
        titleLabel.setFont(StyleConstants.TITLE_FONT);
        titleLabel.setForeground(Color.WHITE);
        titlePanel.add(titleLabel);
        mainPanel.add(titlePanel, BorderLayout.NORTH);

        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new GridLayout(3, 2, 20, 20));
        buttonPanel.setBackground(StyleConstants.BACKGROUND_COLOR);
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));

        customerButton = createStyledButton("客户信息管理");
        usageButton = createStyledButton("用水信息管理");
        paymentButton = createStyledButton("收费信息管理");
        salesmanButton = createStyledButton("业务员信息管理");
        waterTypeButton = createStyledButton("用水类型管理");

        buttonPanel.add(customerButton);
        buttonPanel.add(usageButton);
        buttonPanel.add(paymentButton);
        buttonPanel.add(salesmanButton);
        buttonPanel.add(waterTypeButton);

        mainPanel.add(buttonPanel, BorderLayout.CENTER);
        add(mainPanel);

        // 添加按钮事件监听器
        customerButton.addActionListener(e -> {
            CustomerInfoPanel panel = new CustomerInfoPanel(connection);
            panel.setVisible(true);
        });
        usageButton.addActionListener(e -> {
            UsageInfoPanel panel = new UsageInfoPanel(connection);
            panel.setVisible(true);
        });
        paymentButton.addActionListener(e -> {
            PaymentInfoPanel panel = new PaymentInfoPanel(connection);
            panel.setVisible(true);
        });
        salesmanButton.addActionListener(e -> {
            SalesmanInfoPanel panel = new SalesmanInfoPanel(connection);
            panel.setVisible(true);
        });
        waterTypeButton.addActionListener(e -> {
            WaterTypeInfoPanel panel = new WaterTypeInfoPanel(connection);
            panel.setVisible(true);
        });

        // 连接数据库
        connectToDatabase();
    }

    private JButton createStyledButton(String text) {
        JButton button = new JButton(text);
        button.setFont(StyleConstants.BUTTON_FONT);
        button.setBackground(StyleConstants.BUTTON_COLOR);
        button.setForeground(Color.WHITE);
        button.setFocusPainted(false);
        button.setBorderPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

        // 添加鼠标悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(StyleConstants.BUTTON_COLOR.darker());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(StyleConstants.BUTTON_COLOR);
            }
        });

        return button;
    }

    private void connectToDatabase() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/WaterPay?useSSL=false&serverTimezone=UTC",
                "root",
                "1234"
            );
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this, "数据库连接失败: " + e.getMessage());
            System.exit(1);
        }
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            new WaterBillingSystem().setVisible(true);
        });
    }
}

